<template>
  <div class="my">
    <!-- 未登录头部 -->
    <div v-if="!user" class="title">
      <div class="bg-img"></div>
      <div class="my-info">
        <div class="my-icon">
          <img src="http://liufusong.top:8080/img/profile/avatar.png" alt="" />
        </div>
        <div class="my-user">
          <div class="my-name">游客</div>
          <van-button
            round
            type="primary"
            @click="$router.push('/login')"
            size="mini"
            >去登录</van-button
          >
        </div>
      </div>
    </div>
    <!-- 已登录头部 -->
    <div v-else class="title">
      <div class="login-img"></div>
      <div class="my-info">
        <div class="my-icon">
          <img
            :src="'http://liufusong.top:8080' + userInfoList.avatar"
            alt=""
          />
        </div>
        <div class="my-user">
          <div class="my-name">{{ userInfoList.nickname }}</div>
          <van-button round type="primary" @click="logOut" size="mini"
            >退出</van-button
          >
          <div class="my-edit">
            <span>编辑个人资料 ></span>
          </div>
        </div>
      </div>
    </div>

    <van-grid :column-num="3">
      <van-grid-item @click="goFavorites" icon="star-o" text="我的收藏" />
      <van-grid-item
        icon="wap-home-o"
        to="/rent"
        text="我的出租"
      />
      <van-grid-item icon="clock-o" text="看房记录" />
      <van-grid-item icon="credit-pay" text="成为房主" />
      <van-grid-item icon="contact" text="个人资料" />
      <van-grid-item icon="phone-o" text="联系我们" />
    </van-grid>
    <div class="ad">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="" />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog } from 'vant'
import { getUserInfo } from '@/api/user.js'

export default {
  name: 'my',
  data () {
    return {
      userInfoList: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },

  methods: {
    async loadUserInfo () {
      try {
        const res = await getUserInfo(this.user.token)
        console.log(res)
        this.userInfoList = res.data.body
      } catch (err) {
        console.log(err)
      }
    },
    logOut () {
      Dialog.confirm({
        title: '提示',
        message: '是否确认退出账号'
      })
        .then(() => {
          this.$store.commit('removeUser')
        })
        .catch(() => {})
    },
    goLogin () {
      this.$router.push('/login')
    },
    goFavorites () {
      if (this.user) {
        this.$router.push('/favorites')
      } else {
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my {
  .title {
    min-height: 600px;
    position: relative;
    .bg-img {
      width: 100%;
      height: 380px;
      border-radius: 0 0 100px 100px;
      background-image: url(http://liufusong.top:8080/img/profile/bg.png);
      background-size: cover;
    }
    .login-img {
      width: 100%;
      height: 380px;
      background-image: url(http://liufusong.top:8080/img/avatar.png);
      background-size: cover;
    }
    .my-info {
      position: absolute;
      background: #fff;
      width: 85%;
      height: 55%;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      box-shadow: 0 0 10px 3px #ddd;
      margin: 50px auto 0;
      padding: 0 20px;
      text-align: center;
      font-size: 13px;
      .my-edit {
        margin-top: 20px;
        color: #666;
      }
      .my-icon {
        position: relative;
        transform: translateY(-50%);
        border-radius: 50%;
        width: 70px;
        height: 70px;
        border: 5px solid #f5f5f5;
        display: inline-block;
        box-shadow: 0 2px 2px #bdbdbd;
        img {
          width: 100%;
          border-radius: 50%;
        }
      }
      .my-user {
        padding: 50px 15px;
        .my-name {
          margin-top: -30px;
          margin-bottom: 30px;
        }
        /deep/.van-button {
          display: inline-block;
          padding: 20px 15px;
          /deep/.van-button__text {
            font-size: 13px;
            height: 30px;
            line-height: 30px;
            padding: 20px 15px;
          }
        }
      }
    }
  }
  .van-grid {
    margin-top: 20px;
    .van-hairline--top::after {
      border: none;
    }
    /deep/.van-grid-item {
      [class*="van-hairline"]::after {
        border: none;
      }
    }
  }
  .ad {
    img {
      width: 100%;
    }
  }
}
</style>
